}
/**
- * This function takes two arrays as input, and returns a CGI-style string, e.g.
+ * This function takes one or two arrays, objects, or strings as input, and returns a CGI-style string, e.g.
* "days=7&limit=100". Options in the first array override options in the second.
* Options set to "" will not be output.
- */
-function wfArrayToCGI( $array1, $array2 = NULL )
-{
- if ( !is_null( $array2 ) ) {
- $array1 = $array1 + $array2;
- }
-
- $cgi = '';
- foreach ( $array1 as $key => $value ) {
- if ( '' !== $value ) {
- if ( '' != $cgi ) {
- $cgi .= '&';
+ * @depreciated
+ */
+function wfArrayToCGI( $query1, $query2 = null ) {
+ if( is_null($query2) ) wfBuildQuery( $query1 );
+ else wfBuildQuery( $query2, $query1 );
+}
+/**
+ * wfBuildQuery is a improved wrapper for http_build_query.
+ * We support a defaults array which the query may be merged with. As well we also support
+ * arrays, objects, and strings as input.
+ */
+function wfBuildQuery( $query, $defaults = null ) {
+ if( !is_null($defaults) ) {
+ ## If either array is a string, then parse it and make sure to fix magic quotes.
+ foreach( array( 'query', 'defaults' ) as $var ) {
+ if( is_string($$var) ) {
+ $arr = array();
+ parse_str($$var, &$arr);
+ if( function_exists( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc() ) {
+ global $wgRequest;
+ $wgRequest->fix_magic_quotes( $arr );
+ }
+ $$var = $arr;
}
- $cgi .= urlencode( $key ) . '=' . urlencode( $value );
}
+ # Merge, make sure they are arrays, not objects.
+ $query = ((array)$defaults) + ((array)$query);
}
+
+ # Note that we must specify & because the default is sometimes &
+ $cgi = is_string($query) ? $query : http_build_query( $query, null, '&' );
return $cgi;
}
* Get a cache key
*/
function wfMemcKey( /*... */ ) {
- global $wgDBprefix, $wgDBname;
$args = func_get_args();
- if ( $wgDBprefix ) {
- $key = "$wgDBname-$wgDBprefix:" . implode( ':', $args );
- } else {
- $key = $wgDBname . ':' . implode( ':', $args );
- }
+ $key = wfWikiID() . ':' . implode( ':', $args );
return $key;
}
*/
function wfForeignMemcKey( $db, $prefix /*, ... */ ) {
$args = array_slice( func_get_args(), 2 );
- if ( $prefix ) {
- $key = "$db-$prefix:" . implode( ':', $args );
- } else {
- $key = $db . ':' . implode( ':', $args );
- }
+ wfForeignWikiID($db,$prefix) . ':' . implode( ':', $args );
return $key;
}
* This is used as a prefix in memcached keys
*/
function wfWikiID() {
- global $wgDBprefix, $wgDBname;
+ global $wgDBname, wgDBprefix;
if ( $wgDBprefix ) {
return "$wgDBname-$wgDBprefix";
} else {
}
}
+/**
+ * Get an ASCII string identifying a foreign wiki or shared db
+ * This is used as a prefix in foreign memcached keys
+ */
+function wfForeignWikiID( $db = null, $prefix = null ) {
+ global $wgSharedDB, $wgSharedPrefix, $wgDBname, $wgDBprefix;
+ if( !isset($db) ) $db = (isset($wgSharedDB) ? $wgSharedDB, $wgDBname);
+ if( !isset($prefix) ) $prefix = ($wgSharedPrefix ? $wgSharedPrefix, $wgDBprefix);
+
+ if ( $prefix ) {
+ return "$db-$prefix";
+ } else {
+ return $db;
+ }
+}
+
/**
* Split a wiki ID into DB name and table prefix
*/
*/
public function getFullURL( $query = '', $variant = false ) {
global $wgContLang, $wgServer, $wgRequest;
-
+
+ $query = wfBuildQuery( $query ); # Support query input other than strings.
+
if ( '' == $this->mInterwiki ) {
$url = $this->getLocalUrl( $query, $variant );
-
+
// Ugly quick hack to avoid duplicate prefixes (bug 4571 etc)
// Correct fix would be to move the prepending elsewhere.
if ($wgRequest->getVal('action') != 'render') {
}
} else {
$baseUrl = $this->getInterwikiLink( $this->mInterwiki );
-
+
$namespace = wfUrlencode( $this->getNsText() );
if ( '' != $namespace ) {
# Can this actually happen? Interwikis shouldn't be parsed.
public function getLocalURL( $query = '', $variant = false ) {
global $wgArticlePath, $wgScript, $wgServer, $wgRequest;
global $wgVariantArticlePath, $wgContLang, $wgUser;
-
+
+ $query = wfBuildQuery( $query ); # Support query input other than strings.
+
// internal links should point to same variant as current page (only anonymous users)
if($variant == false && $wgContLang->hasVariants() && !$wgUser->isLoggedIn()){
$pref = $wgContLang->getPreferredVariant(false);
if($pref != $wgContLang->getCode())
$variant = $pref;
}
-
+
if ( $this->isExternal() ) {
$url = $this->getFullURL();
if ( $query ) {
$query = $matches[1];
if( isset( $matches[4] ) ) $query .= $matches[4];
$url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
- if( $query != '' ) $url .= '?' . $query;
+ $url = wfAppendQuery( $url, $query );
}
}
if ( $url === false ) {
$url = "{$wgScript}?title={$dbkey}&{$query}";
}
}
-
+
// FIXME: this causes breakage in various places when we
// actually expected a local URL and end up with dupe prefixes.
if ($wgRequest->getVal('action') == 'render') {